package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.common.constant.JwtClaimsConstant;
import com.heima.common.constant.MessageConstant;
import com.heima.common.exception.LoginFailedException;
import com.heima.common.properties.JwtProperties;
import com.heima.common.result.PageResult;
import com.heima.common.utils.JwtUtil;
import com.heima.mapper.UserMapper;
import com.heima.pojo.dto.UserPageQuery;
import com.heima.pojo.entity.User;
import com.heima.pojo.dto.LoginDto;
import com.heima.pojo.vo.LoginUserVo;
import com.heima.pojo.vo.UserInfoVO;
import com.heima.pojo.vo.UserVO;
import com.heima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtProperties jwtProperties;
    /**
     * 登录
     * @param dto
     * @return
     */
    @Override
    public LoginUserVo login(LoginDto dto) {
        log.info("用户登录数据{}", dto);
        if (dto == null) {
            //参数异常
            throw new LoginFailedException(MessageConstant.ACCOUNT_DISABLED);
        }
        //1.根据用户名查询用户
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getLoginName, dto.getLoginName());
        User user = userMapper.selectOne(wrapper);

        if (user == null) { //代表用户不存在
            throw new LoginFailedException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        if (!dto.getPassword().equals(user.getPassword())) {
            throw new LoginFailedException(MessageConstant.PASSWORD_ERROR);
        }

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        //密码相等
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setUserId(user.getId());
        loginUserVo.setUserName(user.getUserName());
        loginUserVo.setRoleCode(user.getRoleCode());
        loginUserVo.setSuccess(true);
        loginUserVo.setMsg("登录成功");
        loginUserVo.setToken(token);
        return loginUserVo;
    }
    /**
     * 查询人员列表
     * @param userDTO
     * @return
     */
    @Override
    public PageResult<UserVO> pageResult(UserPageQuery userDTO) {
        // 分页
        PageHelper.startPage(userDTO.getPageIndex(), userDTO.getPageSize());
        // 查询
        List<UserVO> userVOS = userMapper.pageResult(userDTO.getUserName());
        //强转 Page
        Page<UserVO> page = (Page<UserVO>) userVOS;
        return PageResult.<UserVO>builder()
                .pageIndex(userDTO.getPageIndex()) //当前页码
                .pageSize(userDTO.getPageSize()) //每页显示条数
                .totalPage(page.getPages()) //总页数
                .totalCount((int) page.getTotal())  //总记录数
                .currentPageRecords(userVOS)
                .build();
    }
    /**
     * 根据id查询人员信息
     * @param id
     * @return
     */
    @Override
    public UserInfoVO getVoById(Integer id) {

      UserInfoVO userInfoVO =  userMapper.getVoById(id);
        return userInfoVO;
    }

}
